import vue from "@vitejs/plugin-vue";
import { fileURLToPath, URL } from "url";
import { defineConfig } from "vite";

export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
      "@components": fileURLToPath(
        new URL("./src/components", import.meta.url),
      ),
      "@demos": fileURLToPath(new URL("./src/demos", import.meta.url)),
    },
    preserveSymlinks: false,
    dedupe: ["vue"],
    extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
  },
  build: {
    cssCodeSplit: false, // UMD should have single CSS file
    emptyOutDir: false, // Don't clean dist, allow multiple builds
    lib: {
      entry: fileURLToPath(new URL("./src/index.ts", import.meta.url)),
      name: "vue3-openlayers",
      formats: ["umd"],
      fileName: (format) => `vue3-openlayers.${format}.js`,
    },
    minify: false,
    rollupOptions: {
      // make sure to externalize deps that should not be bundled
      // into your library
      external: ["vue", /^ol.*/, /^@turf.*/], // Avoid bundling ol imports into the final build
      output: {
        exports: "named",
        globals: {
          vue: "Vue",
          ol: "ol",
          "ol/control": "control",
          "ol/control/Attribution": "Attribution",
          "ol/control/FullScreen": "FullScreen",
          "ol/control/MousePosition": "MousePosition",
          "ol/control/OverviewMap": "OverviewMap",
          "ol/control/ScaleLine": "ScaleLine",
          "ol/control/Rotate": "Rotate$1",
          "ol/control/Zoom": "Zoom",
          "ol/control/ZoomSlider": "ZoomSlider",
          "ol/control/ZoomToExtent": "ZoomToExtent",
          "ol/easing": "animations",
          "ol/extent": "extent",
          "ol/events/condition": "selectconditions",
          "ol/Feature": "Feature",
          "ol/format": "format",
          "ol/functions": "functions",
          "ol/Geolocation": "Geolocation",
          "ol/geom": "geom",
          "ol/geom/Circle": "Circle",
          "ol/geom/Geometry": "Geometry",
          "ol/geom/LineString": "LineString",
          "ol/geom/MultiLineString": "MultiLineString",
          "ol/geom/MultiPoint": "MultiPoint",
          "ol/geom/MultiPolygon": "MultiPolygon",
          "ol/geom/Point": "Point$2",
          "ol/geom/Polygon": "Polygon",
          "ol/interaction": "interaction",
          "ol/interaction/defaults": "defaults",
          "ol/interaction/Draw": "Draw",
          "ol/interaction/DragBox": "DragBox",
          "ol/interaction/DragRotate": "DragRotate",
          "ol/interaction/DragRotateAndZoom": "DragRotateAndZoom",
          "ol/interaction/Link": "Link",
          "ol/interaction/Modify": "Modify",
          "ol/interaction/MouseWheelZoom": "MouseWheelZoom",
          "ol/interaction/Pointer": "Pointer",
          "ol/interaction/Select": "Select",
          "ol/interaction/Snap": "Snap",
          "ol/layer": "Layer",
          "ol/layer/Group": "LayerGroup",
          "ol/layer/Heatmap": "HeatmapLayer",
          "ol/layer/Graticule": "GraticuleLayer",
          "ol/layer/Image": "ImageLayer",
          "ol/layer/Tile": "TileLayer",
          "ol/layer/Vector": "VectorLayer",
          "ol/layer/VectorImage": "VectorImageLayer",
          "ol/layer/VectorTile": "VectorLayerTile",
          "ol/layer/WebGLPoints": "WebGLPointsLayer",
          "ol/layer/WebGLTile": "TileLayer$1",
          "ol/layer/WebGLVector": "WebGLVectorLayer",
          "ol/loadingstrategy": "loadingstrategy",
          "ol/Map": "Map$1",
          "ol/Overlay": "Overlay",
          "ol/proj": "proj",
          "ol/proj/proj4": "proj4$1",
          "ol/proj/Projection": "Projection$1",
          "ol/renderer/webgl/VectorLayer": "WebGLVectorLayerRenderer",
          "ol/source": "source",
          "ol/source/BingMaps": "BingMaps",
          "ol/source/Cluster": "Cluster",
          "ol/source/ImageStatic": "Static",
          "ol/source/Image": "ImageSource",
          "ol/source/ImageWMS": "ImageWMS",
          "ol/source/GeoTIFF": "GeoTIFF",
          "ol/source/OSM": "OSM",
          "ol/source/StadiaMaps": "StadiaMaps",
          "ol/source/Tile": "Tile",
          "ol/source/TileArcGISRest": "TileArcGISRest",
          "ol/source/TileDebug": "TileDebug",
          "ol/source/TileJSON": "TileJSON",
          "ol/source/TileWMS": "TileWMS",
          "ol/source/Vector": "VectorSource",
          "ol/source/VectorTile": "VectorSourceTile",
          "ol/source/WMTS": "WMTSSource",
          "ol/source/XYZ": "XYZ",
          "ol/tilegrid": "tilegrid",
          "ol/tilegrid/WMTS": "TileGridWMTS",
          "ol/View": "View",
          "ol/style/Style": "Style",
          "ol/style/Circle": "CircleStyle",
          "ol/style/Fill": "Fill",
          "ol/style/Stroke": "Stroke",
          "ol/style/Icon": "Icon",
          "ol/style/Text": "Text",
          "ol-contextmenu": "ContextMenu",
          "ol-ext/control/Button": "Button",
          "ol-ext/control/Bar": "Bar",
          "ol-ext/control/LayerSwitcher": "LayerSwitcher",
          "ol-ext/control/LayerSwitcherImage": "LayerSwitcherImage",
          "ol-ext/control/PrintDialog": "PrintDialog",
          "ol-ext/control/Swipe": "Swipe",
          "ol-ext/control/Search": "Search",
          "ol-ext/control/Toggle": "Toggle",
          "ol-ext/control/VideoRecorder": "VideoRecorder",
          "ol-ext/control/MapZone": "MapZone",
          "ol-ext/control/Profile": "Profile",
          "ol-ext/featureanimation/Drop": "Drop",
          "ol-ext/featureanimation/Fade": "Fade",
          "ol-ext/featureanimation/Path": "Path",
          "ol-ext/featureanimation/Shake": "Shake",
          "ol-ext/featureanimation/Slide": "Slide",
          "ol-ext/featureanimation/Teleport": "Teleport",
          "ol-ext/featureanimation/Zoom": "Zoom$1",
          "ol-ext/interaction/SelectCluster": "SelectCluster",
          "ol-ext/interaction/Synchronize": "Synchronize",
          "ol-ext/interaction/Transform": "Transform$1",
          "ol-ext/layer/AnimatedCluster": "AnimatedCluster",
          "ol-ext/style/FlowLine": "FlowLine",
        },
        assetFileNames: (assetInfo) => {
          return assetInfo.name === "main.css"
            ? "styles.css"
            : assetInfo.name || "";
        },
      },
    },
  },
});
